<?php

class EnterpriseWeChat {
    private $corpid; // 企业ID
    private $corpsecret; // 应用的凭证密钥
    private $access_token;

    public function __construct($corpid, $corpsecret) {
        $this->corpid = $corpid;
        $this->corpsecret = $corpsecret;
    }

    // 获取访问凭证
    public function getAccessToken() {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$this->corpid}&corpsecret={$this->corpsecret}";
        $response = file_get_contents($url);
        $data = json_decode($response, true);
        $this->access_token = $data['access_token'];
        return $data['access_token'];
    }

    // 创建成员
    public function createUser($userid, $name, $mobile, $department) {
        if(!$this->access_token){
            $this->getAccessToken();
        }

        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={$this->access_token}";
        $data = array(
            'userid' => $userid,
            'name' => $name,
            'mobile' => $mobile,
            'department' => $department
        );
        $data_string = json_encode($data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/json',
                'content' => $data_string
            )
        );
        $context = stream_context_create($options);
        $response = $this->sendPostRequest($url, false, $context);
        $data = json_decode($response, true);
        return $data;
    }

    // 更新成员
    public function updateUser($userid, $name, $mobile, $department) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={$this->access_token}";
        $data = array(
            'userid' => $userid,
            'name' => $name,
            'mobile' => $mobile,
            'department' => $department
        );
        $data_string = json_encode($data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/json',
                'content' => $data_string
            )
        );
        $context = stream_context_create($options);
        $response = $this->sendPostRequest($url, false, $context);
        $data = json_decode($response, true);
        return $data;
    }

    // 删除成员
    public function deleteUser($userid) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={$this->access_token}&userid={$userid}";
        $response = $this->sendPostRequest($url);
        $data = json_decode($response, true);
        return $data;
    }
	
        //获取部门列表
    public function get_department($id){
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url ="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$this->access_token}&id={$id}";
        $response = file_get_contents($url);
         $result = json_decode($response,true);
        return $result;

    }



    //获取单个部门详情
    public function get_department_detail($id)
    {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url ="https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token={$this->access_token}&id={$id}";
        $response = file_get_contents($url);
        $result = json_decode($response,true);
        return $result;
    }
     //获取部门成员
     public function get_department_details($id){
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={$this->access_token}&department_id={$id}";
        $response = file_get_contents($url);
        $result = json_decode($response,true);
        return $result;
    }
    //获取部门成员详情
    public function get_department_son_detail($department_id=0){
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={$this->access_token}&department_id={$department_id}";
        $response = file_get_contents($url);
        $result = json_decode($response,true);
        return $result;
     }
	
    // 获取部门ID列表 （可用）
    public function getDepartmentIDList($id) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={$this->access_token}&id={$id}";
        $response = $this->sendPostRequest($url);
        $data = json_decode($response, true);
        return $data;
    }

   //获取应用的可见范围
    public function getPermList() {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $data = [];
        $url = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/agent/get_perm_list?access_token={$this->access_token}";
        $response = $this->sendPostRequest($url,$data);
        $data = json_decode($response, true);
        return $data;
    }


       //获取应用的可见范围
       public function getUser($userid) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $data = [
            'userid'=>$userid
        ];
        $url = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/get?access_token={$this->access_token}";
        $response = $this->sendPostRequest($url,$data);
        $data = json_decode($response, true);
        return $data;
    }




    // 获取部门列表
    public function getDepartmentList($department_id=0) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $data = [];
        //$data['department_id'] = $department_id;
        $url = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/department/list?access_token={$this->access_token}";
        $response = $this->sendPostRequest($url,$data);
        $data = json_decode($response, true);
        return $data;
    }

	
    // 获取单个部门详情
    public function getDepartmentIDInfo($id) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token={$this->access_token}&id={$id}";
        $response = file_get_contents($url);
        $data = json_decode($response, true);
        return $data;
    }

    // 获取部门成员
    public function getDepartmentMembers($department_id) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={$this->access_token}&department_id={$department_id}";
        $response = $this->sendPostRequest($url);
        $data = json_decode($response, true);
        return $data;
    }

    // 获取部门成员详情
    public function getDepartmentMemberDetails($department_id) {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={$this->access_token}&department_id={$department_id}";
        $response = $this->sendPostRequest($url);
        $data = json_decode($response, true);
        return $data;
    }

    // 获取成员ID列表
    public function getUserIDList() {
        if(!$this->access_token){
            $this->getAccessToken();
        }
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token={$this->access_token}";
        $response = $this->sendPostRequest($url);
        $data = json_decode($response, true);
        return $data;
    }

    public function sendPostRequest($url, $data=[]) {
        $ch = curl_init($url);
        
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
        $response = curl_exec($ch);
        
        curl_close($ch);
    
        return $response;
    }


    
}

/*
// 使用示例
$corpid = 'your_corpid';
$corpsecret = 'your_corpsecret';
$wechatSDK = new WeChatSDK($corpid, $corpsecret);

// 获取访问凭证
$access_token = $wechatSDK->getAccessToken();

// 创建成员
$userData = array(
    'userid' => 'testuser',
    'name' => 'Test User',
    'mobile' => '123456789',
    'department' => [1]
);
$response = $wechatSDK->createUser($access_token, $userData['userid'], $userData['name'], $userData['mobile'], $userData['department']);
print_r($response);

// 更新成员
$userData['name'] = 'Updated User';
$response = $wechatSDK->updateUser($access_token, $userData['userid'], $userData['name'], $userData['mobile'], $userData['department']);
print_r($response);

// 删除成员
$response = $wechatSDK->deleteUser($access_token, $userData['userid']);
print_r($response);

// 获取部门列表
$response = $wechatSDK->getDepartmentList($access_token);
print_r($response);

// 获取部门成员
$departmentId = 1;
$response = $wechatSDK->getDepartmentMembers($access_token, $departmentId);
print_r($response);

// 获取部门成员详情
$response = $wechatSDK->getDepartmentMemberDetails($access_token, $departmentId);
print_r($response);
*/